#include "mossutil.h"
#include <string>
using namespace std;

bool getFullname(const string& path, string &fullname, int blockno){
	string trans = "";
	int help; int i = 0;
	while(blockno != 0){
		help = blockno % 10;
		blockno /= 10;
		trans.insert(trans.begin(),static_cast<char>(help+'0'));
		++i;
	}
	while (i < NUM_LENGTH)
	{
		trans.insert(trans.begin(),'0');
		++i;
	}
	for(int i = 0;i < NUM_LENGTH - trans.length();i++){
		trans.insert(trans.begin(),'0');
	}
	fullname = path + trans;
	return true;
}

bool parseFullname(const string& fullname, string &path, int* blockno){
	string trans = fullname.substr(fullname.length()-NUM_LENGTH,fullname.length());
	path = fullname.substr(0,fullname.length()-NUM_LENGTH);
	(*blockno) = 0;

	for(int i = 0 ; i < NUM_LENGTH ; i++){
		(*blockno) *= 10;
		(*blockno) += static_cast<int>(trans[i]-'0');
	}

	return true;
}

string getLocalFileName(const string& path, int blockno){
	string fullname;
	getFullname(path,fullname,blockno);
	for( int i = 0 ; i < fullname.length() ; i++ ){
		if(fullname[i] == '/'){
			fullname[i] = '#';
		}
	}
	fullname = "temp/"+ fullname;
	return fullname;
}

bool parsePath(const string& path, string& dir, string& file){
	int last = -1;
	for( int i = 0 ; i < path.length() ; i++ ){
		if(path[i] == '/'){
			last = i;
		}
	}
	dir = path.substr(0,last+1);
	file = path.substr(last+1,path.length());
	return true;
}
